package rkw.database.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import core.database.AbstractEntity;

@Entity
@Table(name = "electionVoter")
public class ElectionVoter extends AbstractEntity {
	@Column(name = "hasVoted", nullable = false)
	private Boolean hasVoted = false;

	@Column(name = "isNonResident", nullable = false)
	private Boolean isNonResident = false;

	@ManyToOne(cascade = { CascadeType.DETACH, CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "voter", insertable = true, updatable = true)
	private Voter voter;

	@ManyToOne
	@JoinColumn(name = "election", insertable = true, updatable = true)
	private Election election;

	public ElectionVoter() {
	}

	public ElectionVoter(Voter voter, Election election) {
		super();
		this.voter = voter;
		voter.getElections().add(this);
		this.election = election;
		election.getVoters().add(this);
	}

	public ElectionVoter(Voter voter, Election election, boolean isNonResident) {
		super();
		this.voter = voter;
		voter.getElections().add(this);
		this.election = election;
		election.getVoters().add(this);
		this.isNonResident = isNonResident;
	}

	public Boolean getHasVoted() {
		return hasVoted;
	}

	public void setHasVoted(Boolean hasVoted) {
		this.hasVoted = hasVoted;
	}

	public Boolean getIsNonResident() {
		return isNonResident;
	}

	public void setIsNonResident(Boolean isNonResident) {
		this.isNonResident = isNonResident;
	}

	public Voter getVoter() {
		return voter;
	}

	public void setVoter(Voter voter) {
		this.voter = voter;
	}

	public Election getElection() {
		return election;
	}

	public void setElection(Election election) {
		this.election = election;
	}

	public void removeFromElection() {
		this.voter.getElections().remove(this);
		this.election.getVoters().remove(this);
	}
}
